<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background-color: #e1e1e1;
        }

        ul {
            background-color: #fff;
            border-radius: 5px;
            width: 1000px;
            margin: 20px auto;
        }

        li {
            height: 60px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #dedede;
            position: relative;
        }

        li:hover {
            background-color: #f6f6f6;
        }

        li img {
            width: 35px;
            height: 35px;
            display: block;
            margin: 0 10px;
            cursor: pointer;
        }

        li p {
            color: #333;
        }

        li p>span:first-child {
            color: #9e78c0;
            font-size: 14px;
        }

        li p>span:last-child {
            font-size: 10px;
            color: #b4b4b4;
        }

        li>span {
            display: block;
            width: 40px;
            height: 20px;
            line-height: 20px;
            text-align: center;
            border-radius: 5px;
            font-size: 12px;
            margin: 0 10px;
            background-color: #e5e5e5;
            color: #999;
        }

        li>span.top {
            background: #80bd01;
            color: #fff;
            font-size: 12px;
        }

        h3 {
            font-weight: 400;
        }

        h3>a {
            color: #333;
            font-size: 16px;
            text-decoration: none;
        }

        h3>a:hover {
            text-decoration: underline;
        }

        time {
            cursor: pointer;
            color: #778087;
            font-size: 11px;
            position: absolute;
            right: 10px;
            top: 20px
        }
    </style>
</head>

<body>
    <div class="nav">
        <a href="">全部</a>
        <a href="">精华</a>
        <a href="">分享</a>
        <a href="">问答</a>
        <a href="">招聘</a>
    </div>

    <ul class="list">

    </ul>
    <ol>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        </ol>
    <script>
        var tab = 'all';
        var page = 1;
        listAjax();
        

        var tabs = ['all', 'good', 'share', 'ask', 'job'];
        var aall = document.querySelectorAll('.nav > a');
        for (let i = 0; i < aall.length; i++) {
            aall[i].onclick = function (e) {
                e.preventDedault();

               tab = tabs[i];
               page = 1;
               listAjax();
            }
        }
        var pages = document.querySelectorAll('ol>li');
        for(let i = 0;i<aall.length;i++){
            pages[i].onclick = function(){
                page= this.innerHTML;
                listAjax()
            }
        }
        function listAjax() {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    var res = JSON.parse(xhr.responseText).data;
                    var str = '';
                    for (var i = 0; i < res.length; i++) {
                        str += `<li>
                            <img src="${res[i].author.avatar_url}" alt="">
                            <p><span>${res[i].reply_count}</span> / <span>${res[i].visit_count}</span></p>
                        `;
                        if (res[i].top) {
                            str += `<span class="top">置顶</span>`;
                        } else if (res[i].good) {
                            str += `<span class="top">精华</span>`;
                        } else if (res[i].tab == "share") {
                            str += `<span>分享</span>`;
                        } else {
                            str += `<span>问答</span>`;
                        }

                        str += `<h3><a href="">${res[i].title}</a></h3>
                        <time>${time(res[i].last_reply_at)}</time>
                        </li>`
                    }
                    var ul = document.querySelector('ul.list');
                    ul.innerHTML = str;
                }
            }
            xhr.open("get", `https://cnodejs.org/api/v1/topics?tab=${tab}&page=${page}`)
            xhr.send();

        }
        function time(lastTime) {
            //得到的时间
            var time1 = new Date().getTime();// 获取当前时间的时间戳
            var time2 = new Date(lastTime).getTime();// 获取参数传递时间的时间戳
            var cha = time1 - time2; // 得到时间戳的差
            var y = parseInt(cha / 1000 / 60 / 60 / 24 / 30 / 12);
            var m = parseInt(cha / 1000 / 60 / 60 / 24 / 30 % 12);
            var d = parseInt(cha / 1000 / 60 / 60 / 24 % 30);
            var h = parseInt(cha / 1000 / 60 / 60 % 24);
            var mi = parseInt(cha / 1000 / 60 % 60);

            if (y) {
                return y + "年前"
            } else if (m) {
                return m + "月前"
            } else if (d) {
                return d + "天前"
            } else if (h) {
                return h + "小时前"
            } else if (mi) {
                return mi + "分钟前"
            } else {
                return "刚刚";
            }

        }
    </script>
</body>

</html>